import pygame
import json

def draw_path_on_image(map_image_path, path_points_path, output_image_path):
    # 初始化pygame
    pygame.init()

    # 加载地图图片
    map_image = pygame.image.load(map_image_path)
    original_width, original_height = map_image.get_size()

    # 设置窗口为最大化
    screen = pygame.display.set_mode((0, 0), pygame.RESIZABLE)
    pygame.display.set_caption("Map Path Viewer")
    screen_width, screen_height = pygame.display.get_surface().get_size()

    # 计算地图的缩放比例，保持宽高比
    map_aspect_ratio = original_width / original_height

    # 地图只占屏幕的上十分之九区域
    target_height = int(screen_height * 0.9)  # 上十分之九的高度
    target_width = int(target_height * map_aspect_ratio)

    # 如果地图宽度超过屏幕宽度，按宽度缩放
    if target_width > screen_width:
        target_width = screen_width
        target_height = int(target_width / map_aspect_ratio)

    # 缩放地图图片
    scaled_map_image = pygame.transform.scale(map_image, (target_width, target_height))

    # 计算地图在屏幕上的居中位置（仅在上十分之九区域居中）
    map_x = (screen_width - target_width) // 2
    map_y = 0  # 地图从屏幕顶部开始显示

    # 创建一个新的Surface，用于绘制路径
    path_image = pygame.Surface((target_width, target_height), pygame.SRCALPHA)
    path_image.fill((0, 0, 0, 0))  # 透明背景

    # 加载路径点和更换地图的指定位置
    with open(path_points_path, 'r') as f:
        data = json.load(f)
        path_points = data["path_points"]
        # 将字符串键转换回元组
        map_change_points = {eval(k): v for k, v in data["map_change_points"].items()}

    # 绘制路径
    if len(path_points) > 0:
        # 将路径点从原始分辨率映射到缩放后的分辨率
        scaled_path_points = [
            (
                int(x * (target_width / original_width)),
                int(y * (target_height / original_height))
            )
            for x, y in path_points
        ]

        # 绘制第一个点
        start_x, start_y = scaled_path_points[0]
        pygame.draw.circle(path_image, (255, 0, 0), (start_x, start_y), 5)

        # 绘制后续点和线段
        for i in range(1, len(scaled_path_points)):
            end_x, end_y = scaled_path_points[i]
            pygame.draw.circle(path_image, (255, 0, 0), (end_x, end_y), 5)
            pygame.draw.line(path_image, (255, 0, 0), (start_x, start_y), (end_x, end_y), 2)
            start_x, start_y = end_x, end_y

    # 绘制更换地图的指定位置
    for point, _ in map_change_points.items():
        scaled_x = int(point[0] * (target_width / original_width))
        scaled_y = int(point[1] * (target_height / original_height))
        pygame.draw.circle(path_image, (0, 0, 255), (scaled_x, scaled_y), 5)

    # 将路径绘制到缩放后的地图图片上
    scaled_map_image.blit(path_image, (0, 0))

    # 清屏并显示地图和路径
    screen.fill((0, 0, 0))  # 填充黑色背景
    screen.blit(scaled_map_image, (map_x, map_y))
    pygame.display.flip()

    # 保存生成的新图片
    pygame.image.save(scaled_map_image, output_image_path)
    print(f"Path drawn on map and saved to {output_image_path}")

    # 主循环，等待用户关闭窗口或按空格键退出
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                running = False

    # 退出pygame
    pygame.quit()

if __name__ == "__main__":
    # 输入文件路径
    map_image_path = "assets/maps/map5.png"  # 原始地图图片路径
    path_points_path = "path_points.json"    # 路径点文件路径
    output_image_path = "test/path_on_map.png"    # 输出图片路径

    # 绘制路径并生成新图片
    draw_path_on_image(map_image_path, path_points_path, output_image_path)